package cn.gaotu.org.controller;

import cn.gaotu.basic.excepion.BusinessException;
import cn.gaotu.basic.util.ExcelUtil;
import cn.gaotu.org.domain.ShopAuditLog;
import cn.gaotu.org.vo.ShopVo;
import cn.gaotu.org.service.ShopService;
import cn.gaotu.org.domain.Shop;
import cn.gaotu.org.query.ShopQuery;
import cn.gaotu.basic.util.AjaxResult;
import cn.gaotu.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop")
public class ShopController {

    /**
     *
     */
    @Autowired
    public ShopService shopService;
    @GetMapping("/echarts")
    public Map<String,Object> echartsData(){
        List<ShopVo> shopVos=shopService.countByState();
        //把其中的数据转换为list或者数组  前端用于图表的y轴数据
        List<Integer> nums = shopVos.stream().map(ShopVo::getCountNum).collect(Collectors.toList());
        List<Integer> states = shopVos.stream().map(ShopVo::getState).collect(Collectors.toList());
        Map<String,Object> map=new HashMap<>();
        map.put("x", states);
        map.put("y",nums);
        return map;
    }

    /**
     * 导入excel表格
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，
        List<Shop> list = ExcelUtil.importExcel(file,1,1,Shop.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+list.size()+"】行");
        //保存到数据库中[自己实现，提示：动态sql 批量插入]

    }

    /**
     * 导出excel表格
     * @param response
     */
    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.findAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 激活店铺
     * @param id
     * @return
     */
    @GetMapping("/passUpdate/{id}")
    public String passUpdate(@PathVariable("id") Long id) {
        Shop shop = shopService.findById(id);
        shop.setState(3);
        shopService.update(shop);
        return "激活成功";
    }
    /*
    * 审核通过
    * */
    @PostMapping("/audit/pass")
    public AjaxResult reject(@RequestBody ShopAuditLog log) {
        try {
            shopService.pass(log);
            return new AjaxResult();
        } catch (BusinessException e) { //捕捉自己抛出的异常
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) { //500
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 审核驳回
     * @param log
     * @return
     */
    @PostMapping("/audit/reject")
    public AjaxResult pass(@RequestBody ShopAuditLog log ) {
        try {
            shopService.reject(log);
            return new AjaxResult();
        } catch (BusinessException e) { //捕捉自己抛出的异常
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) { //500
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }
    //商铺注册
    @PostMapping(value="/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (BusinessException e) { //捕捉自己抛出的异常
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) { //500
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }



    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.DeleteDep(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
                shopService.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id")Long id) {
        return shopService.findById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> findAll(){
        return shopService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }
}
